import {keyBoradMap} from "../type/Type";
import {GlobalEnv} from "../utils/GlobalEnv";
import {GuiControllerOpt, GuiId, TimeControllerOpt} from "../controller/model/ControllerOpt";
import {GuiController, OptType} from "../controller/GuiController";
import Configer from "../frame/apiModel/config/Configer";
import {ConfigKey} from "../constant/ConfigConstant";
import {SyncDataKey, TriggerFactory} from "../frame/TriggerFactory";
import {PlayerModel} from "../frame/apiModel/adapterModel/model4/PlayerModel";
import {CodeUtil} from "../frame/codeAdapter/CodeUtil";
import {ResourceKey} from "../constant/ResourceConstant";
import {FinalKey} from "../constant/FinalConstant";
import {UnitController} from "../controller/UnitController";
import {MouseEventInfo, triggetAdapter} from "../frame/apiModel/adapter/adapterInterface/model4/ITriggerAdapter";
import {LocModel} from "../frame/apiModel/base/LocModel";
import DSan from "./ability/DSan";
import {PlayerDataKey} from "../frame/dataSource/DataSource";
import {PlayerFactory} from "../frame/PlayerFactory";
import {TimeController} from "../controller/TimeController";
import {Logger} from "../frame/apiModel/config/Logger";

export function keyBoradTrigger() {
    backHomeTrigger();
    attrTrigger();
    bagTrigger();
    achBarTrigger();
    talentBarTrigger();
    damagePanelTrigger();
    petTrigger();
    handBookTrigger();
    appearanceTrigger();
    escTrigger();
    skillTrigger();
    spaceTrigger();
    dSanTrigger()
}

function dSanTrigger() {
    const useDSan = Configer.getConfig(ConfigKey.useDSan);
    if (!CodeUtil.isNullOrZero(useDSan)) {
    } else {
        return;
    }
    const dSan = new DSan();
    const callBack = function (this: void, player: PlayerModel, data: any) {
        const key = data.key;
        if (key === 'D') {
            const x = data.x;
            const y = data.y;
            const loc = new LocModel(x, y);
            const selectedUnit = player.getLastSelectedUnit();
            if (selectedUnit?.isAliveInBattle()) {
                dSan.actionFun(selectedUnit, undefined, undefined, undefined, player, undefined, undefined, loc);
            }
        }
    }
    TriggerFactory.getInstance().registerSyncData(SyncDataKey.keyEvent, callBack)


    const followMouse = function () {
        PlayerFactory.getInstance().mapAllNotEndPlayingPlayer(
            (player: PlayerModel) => {
                const bFollow = player.data[PlayerDataKey.bFollowMouse]
                if (!CodeUtil.isNullOrZero(bFollow)) {
                    const selectUnit = player.getLastSelectedUnit();
                    if (selectUnit?.isAliveInBattle()) {
                        const range = 100;
                        const loc = selectUnit?.loc;
                        const mouseEventInfo: MouseEventInfo = triggetAdapter.adapter.getMouseEventInfo();
                        const mouseLoc = mouseEventInfo.loc;
                        if (loc?.isVaild()) {
                            const distance = loc.getAbsDistance(mouseLoc);
                            if (distance > range) {
                                const data = { x: mouseLoc.x, y: mouseLoc.y, key: 'D' };
                                TriggerFactory.getInstance().SyncObjData(SyncDataKey.keyEvent, data)
                            }
                        }
                    }
                }
                player.data[PlayerDataKey.bFollowMouse] = 0;
            }
        )
    }
    const foreverPeriodic = Configer.getFinal(FinalKey.foreverPeriodic) as number;
    const followMouseOpt = new TimeControllerOpt(undefined, followMouse, foreverPeriodic);
    TimeController.getInstance().startTimer({time: 0.01, opt: followMouseOpt});

    const dShanCallBack = function (this: void, player: PlayerModel) {
        if (player.isLocalPlayer()) {
            player.data[PlayerDataKey.bFollowMouse] = 1
        }
    }
    TriggerFactory.getInstance().registerKeyEvent(keyBoradMap.VK_D, true, dShanCallBack)
}

function backHomeTrigger() {
    const useF2BackHome = Configer.getConfig(ConfigKey.useF2BackHome);
    if (!CodeUtil.isNullOrZero(useF2BackHome)) {
    } else {
        return;
    }
    const callBack = function (this: void, player: PlayerModel) {
        UnitController.getInstance().backHome(player);
    }
    TriggerFactory.getInstance().registerKeyEvent(keyBoradMap.VK_F2, true, callBack)
}
function attrTrigger() {
    const useAttrBar = Configer.getConfig(ConfigKey.useAttrBar);
    const button: string = Configer.getResource(ResourceKey.attrButton);
    if (!CodeUtil.isNullOrZero(useAttrBar) && !CodeUtil.isNullOrZero(button)) {
    } else {
        return;
    }
    const callBack = function (this: void, player: PlayerModel) {
        GuiController.getInstance().showGui(new GuiControllerOpt(GuiId.attrBar, {}), player, OptType.reverse);
    }
    TriggerFactory.getInstance().registerKeyEvent(keyBoradMap.VK_TAB, true, callBack)
}
function spaceTrigger() {
    const callBack = function (this: void, player: PlayerModel) {
        const hero = GlobalEnv.getHero(player);
        if (hero) {
            player.moveCamera(hero.loc, 0);
        }
    }
    TriggerFactory.getInstance().registerKeyEvent(keyBoradMap.VK_SPACE, true, callBack)
}
function achBarTrigger() {
    // const useAchBar = Configer.getConfig(ConfigKey.useAchBar);
    // const button: string = Configer.getUIConfig(ResourceKey.achButton);
    // if (useAchBar > 0  && button && button.length && button.length > 0) {
    // } else {
    //     return;
    // }
    // const callBack = function (this: void, player: PlayerModel) {
    //     GuiController.getInstance().uiEnterFun(player, GuiId.achievementBar);
    // }
    // TriggerFactory.getInstance().registerKeyEvent(keyBoradMap.VK_L, callBack)
}
function talentBarTrigger() {
    // const useTalentBar = Configer.getConfig(ConfigKey.useTalentBar);
    // const button: string = Configer.getUIConfig(ResourceKey.talentButton);
    // if (useTalentBar > 0  && button && button.length && button.length > 0) {
    // } else {
    //     return;
    // }
    // const callBack = function (this: void, player: PlayerModel) {
    //     GuiController.getInstance().uiEnterFun(player, GuiId.talentBar);
    // }
    // TriggerFactory.getInstance().registerKeyEvent(keyBoradMap.VK_K, callBack)
}
function bagTrigger() {
    // const useBagBar = Configer.getConfig(ConfigKey.useGoodsBar);
    // const button: string = Configer.getUIConfig(ResourceKey.bagButton);
    // if (useBagBar > 0  && button && button.length && button.length > 0) {
    // } else {
    //     return;
    // }
    const callBack = function (this: void, player: PlayerModel) {
        GuiController.getInstance().showGui(new GuiControllerOpt(GuiId.goodsBar, {}), player, OptType.reverse);
    }
    TriggerFactory.getInstance().registerKeyEvent(keyBoradMap.VK_B, true, callBack)
}
function damagePanelTrigger() {
    // const useBar = Configer.getConfig(ConfigKey.useDamageBar);
    // const button: string = Configer.getUIConfig(ResourceKey.damageButton);
    // if (useBar > 0  && button && button.length && button.length > 0) {
    // } else {
    //     return;
    // }
    // const callBack = function (this: void, player: PlayerModel) {
    //     GuiController.getInstance().uiEnterFun(player, GuiId.damageButton);
    // }
    // TriggerFactory.getInstance().registerKeyEvent(keyBoradMap.VK_J, callBack)
}
function petTrigger() {
    // const useBagBar = Configer.getConfig(ConfigKey.usePetBar);
    // const button: string = Configer.getUIConfig(ResourceKey.petButton);
    // if (useBagBar > 0  && button && button.length && button.length > 0) {
    // } else {
    //     return;
    // }
    // const callBack = function (this: void, player: PlayerModel) {
    //     GuiController.getInstance().uiEnterFun(player, GuiId.petBar);
    // }
    // TriggerFactory.getInstance().registerKeyEvent(keyBoradMap.VK_I, callBack)
}

function handBookTrigger() {
    // const useBar = Configer.getConfig(ConfigKey.useHandBook);
    // const button: string = Configer.getUIConfig(ResourceKey.handBookButton);
    // if (useBar > 0  && button && button.length && button.length > 0) {
    // } else {
    //     return;
    // }
    // const callBack = function (this: void, player: PlayerModel) {
    //     GuiController.getInstance().uiEnterFun(player, GuiId.handBookBar);
    // }
    // TriggerFactory.getInstance().registerKeyEvent(keyBoradMap.VK_I, callBack)
}

function appearanceTrigger() {
    // const useBar = Configer.getConfig(ConfigKey.useAppearance);
    // const button: string = Configer.getUIConfig(ResourceKey.appearanceButton);
    // if (useBar > 0  && button && button.length && button.length > 0) {
    // } else {
    //     return;
    // }
    // const callBack = function (this: void, player: PlayerModel) {
    //     GuiController.getInstance().uiEnterFun(player, GuiId.appearanceBar);
    // }
    // TriggerFactory.getInstance().registerKeyEvent(keyBoradMap.VK_M, callBack)
}

function escTrigger() {
    const callBack = function (this: void, player: PlayerModel) {
        GuiController.getInstance().escFun(player);
    }
    TriggerFactory.getInstance().registerKeyEvent(keyBoradMap.VK_ESC, true, callBack)
}

function skillTrigger() {
    const keys: string[] = Configer.getFinal(FinalKey.skillHotKeys);

    const fun = function (this: void, keyBorad: keyBoradMap, key: string) {
        const callBack = function (this: void, player: PlayerModel) {
            GuiController.getInstance().checkUseAbility(player, key);
        }
        TriggerFactory.getInstance().registerKeyEvent(keyBorad, true, callBack)
    }

    for (let i = 0; i < keys.length; i++) {
        const key = keys[i];
        switch (key) {
            case 'Q': {
                fun(keyBoradMap.VK_Q, key)
                break;
            }
            case 'W': {
                fun(keyBoradMap.VK_W, key)
                break;
            }
            case 'E': {
                fun(keyBoradMap.VK_E, key)
                break;
            }
            case 'R': {
                fun(keyBoradMap.VK_R, key)
                break;
            }
            case 'T': {
                fun(keyBoradMap.VK_T, key)
                break;
            }
            case 'Y': {
                fun(keyBoradMap.VK_Y, key)
                break;
            }
        }
    }
}